﻿using B.S.BaseData.Infrastructure.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace B.S.BaseData.Infrastructure.Implement
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly DBContext db;

        public BaseRepository(DBContext db)
        {
            this.db = db;
        }
        public int Add(T entity)
        {
            db.Set<T>().Add(entity);
            return db.SaveChanges();
        }

        public Task<int> AddAsync(T entity)
        {
            db.Set<T>().AddAsync(entity);
            return db.SaveChangesAsync();
        }

        public int AddRange(List<T> entities)
        {
           db.Set<T>().AddRange(entities);
            return db.SaveChanges();
        }

        public int Del(T entity)
        {
           db.Set<T>().Remove(entity);
            return db.SaveChanges();
        }

        public IQueryable<T> GetAll()
        {
           return db.Set<T>().AsQueryable();
        }

        public int Update(T entity)
        {
            db.Set<T>().Update(entity);
            return db.SaveChanges();
        }

        public async Task<int> UpdateAsync(T entity)
        {
            db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }

        public async Task<int> DelRang(List<T> entity)
        {
            db.Set<T>().RemoveRange(entity);
            return await db.SaveChangesAsync();
        }
    }
}
